﻿// Explosion sprite sheet by Daniel Steegmüller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Asteroids
{
    class ShipExplosion : GameObject
    {
        private SpriteBatch spriteBatch;
        private Texture2D sprite;

        public float timeAlive { get; private set; }
        public float Scale;
        Color explosionColor;

        float timer = 0f;
        float interval = 70f;
        int currentFrameRow = 1;
        int currentFrameColumn = 1;
        //Width of a single sprite image
        int spriteWidth = 120;
        //Height of a single sprite image
        int spriteHeight = 120;
        Rectangle sourceRect;
        Vector2 center;

        public ShipExplosion(Game g, Vector2 position, float timeMade, float scale, Color eColor)
            : base(g)
        {
            Position = position;
            Velocity = new Vector2(0, 0);
            timeAlive = timeMade;
            Scale = scale;
            explosionColor = eColor;
        }

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("RedExplosion");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            //SoundEffect soundEffect;
            //soundEffect = Game.Content.Load<SoundEffect>("ship_explosion");
            //soundEffect.Play();
        }

        public override void Draw(GameTime gameTime)
        {

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                //Show the next frame
                currentFrameColumn++;
                //Reset the timer
                timer = 0f;
            }

            if (currentFrameColumn == 3)
            {
                currentFrameColumn = 0;
                currentFrameRow++;
            }
            if (currentFrameRow == 4)
            {
                currentFrameRow = 1;
            }

            sourceRect = new Rectangle(currentFrameColumn * spriteWidth, currentFrameRow * spriteHeight, spriteWidth, spriteHeight);
            center = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);

            spriteBatch.Begin();
            spriteBatch.Draw(sprite, 
                             Position,
                             sourceRect, 
                             explosionColor,
                             0f, 
                             center, 
                             Scale,
                             SpriteEffects.None, 
                             0);
            spriteBatch.End();
        }
    }
}
